package com.bdqn.tour.servlet.Hzy;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.bdqn.tour.util.HzyAliPay.AlipayConfig;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/alipay/pay")
public class PayServlet extends HttpServlet {
    AlipayClient client;

    @Override
    public void init() throws ServletException {
        client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        try {
            // 获取订单信息
            String orderId = req.getParameter("orderId");
            String orderType = req.getParameter("orderType");
            String money = req.getParameter("money");

            // 参数校验
            if (orderId == null || orderType == null || money == null) {
                throw new ServletException("订单参数不完整");
            }

            // 生成标准格式的商户订单号
            String outTradeNo = "H" + orderId; // 使用H前缀，确保格式统一

            // 打印订单信息用于调试
            System.out.println("订单信息：");
            System.out.println("原始订单ID: " + orderId);
            System.out.println("订单类型: " + orderType);
            System.out.println("支付金额: " + money);
            System.out.println("商户订单号: " + outTradeNo);

            // 创建支付请求
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();

            // 设置同步回调地址
            alipayRequest.setReturnUrl(AlipayConfig.return_url);
            // 设置异步通知地址
            alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

            // 构建业务参数
            AlipayTradePagePayModel model = new AlipayTradePagePayModel();
            model.setOutTradeNo(outTradeNo);
            model.setSubject("旅游订单-" + orderType);
            model.setTotalAmount(money);
            model.setBody("旅游订单支付");
            model.setProductCode("FAST_INSTANT_TRADE_PAY");
            model.setTimeoutExpress("2m"); // 设置超时时间

            // 设置业务参数
            alipayRequest.setBizModel(model);

            // 调用SDK生成表单
            String form = client.pageExecute(alipayRequest).getBody();

            // 输出表单
            PrintWriter out = resp.getWriter();
            out.write(form);
            out.flush();
            out.close();

        } catch (AlipayApiException e) {
            e.printStackTrace();
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "支付宝接口调用失败");
        } catch (Exception e) {
            e.printStackTrace();
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "系统错误");
        }
    }
}
